<?php
namespace app\model;
use think\facade\Cache;
use think\Model;


class Admin extends Model
{
    protected $name = 'admin';
    /**
     * 新增编辑管理员
     */
    public  function addEditAdmin($param){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        if (!isset($param['user_name']) || !isset($param['password']) ||!isset($param['role_ids'])){
            return getFailResponse('参数有误');
        }

        $adminInfo = self::where('user_name',$param['user_name'])->find();
        $data = [
            'user_name'=>$param['user_name'],
            'password'=>sha1($param['password']),
            'create_time'=>time(),
            'update_time'=>time(),
        ];
        if (empty($param['role_ids'])){
            return getFailResponse('请选择角色');
        }
        if (isset($param['id']) && $param['id'] >0 ){
            if (empty($param['user_name'])){
                return getFailResponse('参数有误');
            }
            unset($data['create_time']);
            if (empty($param['password'])){
                unset($data['password']);
            }
            if (empty($adminInfo) && $adminInfo['id'] != $param['id']){
                return getFailResponse('用户名已存在');
            }
            $saveAdmin =  self::where(['id'=>$param['id']])->update($data);
            if ($saveAdmin){
                $admin_id = $param['id'];
                AdminRoles::where(['admin_id'=>$admin_id])->delete();
                $roleIdArr = explode(',', $param['role_ids']);
                foreach ($roleIdArr as $val){
                    $roleData =[
                        'admin_id'=>$admin_id,
                        'role_id'=>$val,
                        'create_time'=>time(),
                        'update_time'=>time(),
                    ];
                    AdminRoles::insert($roleData);
                }
            }
        }else{
            if (empty($param['user_name']) || empty($param['password'])){
                return getFailResponse('参数有误');
            }
            if ($adminInfo){
                return getFailResponse('用户名已存在');
            }
            $saveAdmin =  self::create($data);
            if ($saveAdmin){
                $admin_id = $saveAdmin->id;
                $roleIdArr = explode(',', $param['role_ids']);
                foreach ($roleIdArr as $val){
                    $roleData =[
                        'admin_id'=>$admin_id,
                        'role_id'=>$val,
                        'create_time'=>time(),
                        'update_time'=>time(),
                    ];
                    AdminRoles::insert($roleData);
                }
            }
        }
        return getSuccessResponse();
    }
    /**
     * 管理员登陆
     */
    public function  adminLogin($param=[]){
        if (!isset($param['user_name']) || !isset($param['password'])){
            return getFailResponse('参数有误');
        }
        $adminInfo = self::where(['user_name'=>$param['user_name']])->find();
        if (!$adminInfo){
            return getFailResponse('用户名错误');
        }
        if (sha1($param['password']) != $adminInfo->password){
            return getFailResponse('密码错误');
        }

        $expireSecond = 8640000;
        $configModel = new Config();
        $token = $configModel->getToken('','',$adminInfo->id,$expireSecond);
        Cache::set('admin_token'.$adminInfo['id'],$token,$expireSecond);
        $result = [
            'token'=>$token
        ];
        return getSuccessResponse('登陆成功',$result);
    }

    /**
     * 管理员退出登陆
     */
    public function  adminLoginOut($param=[]){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        Cache::delete('admin_token'.$param['admin_id']);
        return getSuccessResponse('退出成功');
    }
    /**
     * 管理员列表
     * $param
     */
    public function  adminList($param =[]){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $pageSize = $param['page_size'] ?? 15 ;
        $page = $param['page'] ?? 1;
        $total = self::count();
        $list = self::page($page,$pageSize)->order('id','desc')->select();
        foreach ($list as $key=>&$val){
            $roles = AdminRoles::where(['admin_id'=>$val['id']])->select()->toArray();
            $val['role_ids'] =implode(',', array_column($roles,'role_id'));
        }
        $result = [
            'total'=>$total,
            'list'=>$list,
        ];
        return getSuccessResponse('查询成功',$result);
    }

    /**
     * 删除管理员
     */
    public function  delAdmin($param=[]){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $ids = $param['ids'] ?? '';
        if (empty($ids)){
            return getFailResponse('参数有误');
        }
        $idsArr = explode(',',$ids);
        foreach ($idsArr as $val){
            self::where(['id'=>$val])->delete();
            AdminRoles::where(['admin_id'=>$val])->delete();
        }
        return getSuccessResponse('删除成功');
    }

    /**
     * 管理员详情
     */
    public function  adminDetail($param=[]){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $id = $param['id'] ?? '';
        if (empty($id)){
            return getFailResponse('参数有误');
        }
        $detail = self::where(['id'=>$id])->find();
        unset($detail['password']);
        return getSuccessResponse('查询成功',$detail);
    }

    /**
     * 获取管理员信息
     */
    public function  getAdminInfo($param=[]){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $detail = self::where(['id'=>$param['admin_id']])->find();
        unset($detail['password']);
        return getSuccessResponse('查询成功',$detail);
    }
}